<?php
namespace BWL\webshopBundle\DataFixtures\ORM;
/**
 * Created by JetBrains PhpStorm.
 * User: leon
 * Date: 11/25/12
 * Time: 12:02 PM
 * To change this template use File | Settings | File Templates.
 */

use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use BWL\webshopBundle\Entity\Ordering;

class OrderingFixtures extends AbstractFixture implements OrderedFixtureInterface
{
  public function load(ObjectManager $manager) {

    $items = array(
      0 => 'itemA',
      1 => 'itemB',
      2 => 'itemC',
      3 => 'itemD',
      4 => 'itemE',
      5 => 'itemF',
      6 => 'itemG',
      7 => 'itemH',
      8 => 'itemI',
      9 => 'itemJ',
      10 => 'itemK',
      11 => 'itemL',
      12 => 'itemM',
      13 => 'itemN',
    );
    $users = array(
      0 => 'userErwin',
      1 => 'userLeon',
      2 => 'userChristian',
      3 => 'userSven',
      4 => 'userJannik',
    );
    //generate ordering
    for($orderId = 0; $orderId < 1000; $orderId++) {
      $itemCount = rand(1, 10);
      $orderUser = $users[rand(0, 4)];
      $orderItems_ary = array();
      //Order items
      for($orderItems = 0; $orderItems < $itemCount; $orderItems++) {
        $orderItem = $items[rand(0, 13)];
        if(!in_array($orderItem, $orderItems_ary)) {
          array_push($orderItems_ary, $orderItem);
          $ordering = new Ordering();
          $ordering->setId($orderId);
          $ordering->setItem($manager->merge($this->getReference($orderItem)));
          $ordering->setUser($manager->merge($this->getReference($orderUser)));
          $ordering->setState($manager->merge($this->getReference('closed')));
          $ordering->setCount(rand(1, 5));
          $ordering->setOrderDate(new \DateTime("2012-".rand(1,12)."-".rand(1,28)." ".rand(0,24).":".rand(0,59).":".rand(0,59)));
          $ordering->setShipmentDate($ordering->getOrderDate()->modify('+1 day'));
          $ordering->setPrice($manager->merge($this->getReference($orderItem))->getSalePrice());
          $manager->persist($ordering);
        }
      }
     // $manager->flush();
   //   $manager->clear();
    }

    $manager->flush();
  }


  public function getOrder() {
    return 6;
  }
}
